﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ByiaaMall.Travelzen.Common
{
    /// <summary>
    /// 参数配置 可通过配置文件配置 
    /// </summary>
    public class EmailHelper
    {
        /// <summary>
        /// 
        /// </summary>
        static string _userName="";
        static string UserName
        {
            get
            {
                if(string.IsNullOrEmpty(_userName))
                {
                    _userName = System.Configuration.ConfigurationManager.AppSettings["Email_UserName"];
                }
                return _userName;
            }
        }

        static string _password="";
        static string Password
        {
            get
            {
                if (string.IsNullOrEmpty(_password))
                {
                    _password = System.Configuration.ConfigurationManager.AppSettings["Email_Passwrod"];
                }
                return _password;
            }
        }

        static string _smtpServer = "smtp.ym.163.com";
        static string SmtpServer
        {
            get
            {
                if (string.IsNullOrEmpty(_smtpServer))
                {
                    _smtpServer = System.Configuration.ConfigurationManager.AppSettings["Email_SmtpServer"];
                }
                return _smtpServer;
            }
        }

        static int _port = 25;
        static int Port
        { 
            get
            {
                if (_port == 0)
                {
                    _port =int.Parse(System.Configuration.ConfigurationManager.AppSettings["Email_Port"]);
                }
                return _port; 
            }
        }


        /// <summary>
        /// 发送邮件  暂时没有发送附件 需要 需要再加
        /// </summary>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="to">发送邮箱列表</param>
        /// <param name="IsBodyHtml">是否支持html显示</param>
        /// <returns></returns>
        public bool SendEmail(string subject, string body, IList<string> to, bool IsBodyHtml)
        {
            try
            {
                System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
                smtpClient.Credentials = new System.Net.NetworkCredential(UserName, Password);
                smtpClient.Host = SmtpServer;
                smtpClient.Port = Port;
                var message = new System.Net.Mail.MailMessage();
                message.Body = body;
                message.From = new System.Net.Mail.MailAddress(UserName);
                var list = new System.Net.Mail.MailAddressCollection();
                foreach(var item in to)
                {
                    message.To.Add(item);
                }
                message.Subject = subject;
                message.IsBodyHtml = IsBodyHtml;
                smtpClient.Send(message);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
